package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ContentRecommendationsEntityFacade;
import dao.ContentRecommendationsEntityFacadeLocal;
import entities.ContentEntity;
import entities.ContentRecommendationsEntity;
import entities.UserEntity;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import utils.EmailChecker;

/**
 * 
 * @author andrei costache
 *
 */
public class RecommendContentController extends ContentController {

	@EJB ContentRecommendationsEntityFacadeLocal recommandationDAO;

	@Override
	public void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
            RequestDispatcher view = null;
            String cid = request.getParameter("cid");
            if (cid != null){
                ContentEntity content = contentDAO.find(new Long(cid));
                //Check if a new comment has to be stored.
                String submitted = request.getParameter("submitted");
                if (submitted != null){
                    String email = request.getParameter("email");
                    String message = request.getParameter("message");
                    boolean ok = true;
                    if(!EmailChecker.isEmailValid(email)){
                        ok = false;
                        msg.addStatusMessage("Please enter a valid email!");
                    }
                    if (message.trim().equals("")){
                        ok = false;
                        msg.addStatusMessage("Please enter a message!");
                    }
                    if (ok){
                        ContentRecommendationsEntity recommendation = new ContentRecommendationsEntity();
                        System.out.println(recommendation);
                        HttpSession session = request.getSession();
                        recommendation.setUid(((UserEntity)session.getAttribute("user")).getUid());
                        System.out.println(recommendation);
                        recommendation.setCid(Long.parseLong(cid));
                        System.out.println(recommendation);
                        recommendation.setCreated(System.currentTimeMillis()/1000);
                        System.out.println(recommendation);
                        recommendation.setEmail(email);
                        System.out.println(recommendation);
                        recommendation.setMessage(message);
                        System.out.println(recommendation);
                        recommandationDAO.create(recommendation);
                    }
                    request.setAttribute("messages", msg);
                    view = request.getRequestDispatcher("/jsp/fragments/RecommendForm.jsp");
                } else {
                    view = request.getRequestDispatcher("/jsp/ContentFullView.jsp");
                }
                //Inject the content and the comments.
                request.setAttribute("content", content);
                view.forward(request, response);
            }
        }
}
